const {UserLogin} = require('../models/login')
const bcrypt = require('bcryptjs')

class UserLoginDao {
  // 新增广告
  static async create(v) {
    const login = new UserLogin();
    login.is_student = v.is_student ? v.is_student : 1
    login.number = v.number
    login.mail = v.mail
    login.account = v.account
    login.nickname = v.nickname
    login.pwd = v.pwd
    login.status = v.status
    login.work_number = v.workNumber ? v.workNumber : ''
    login.updated_at = ''
    login.created_at = new Date()

    return login.save();
  }

  // 删除广告
  static async destroy(id) {
    const advertise = await UserLogin.findOne({
      where: {
        id,
      }
    });
    if (!advertise) {
      throw new global.errs.NotFound('没有找到该用户');
    }
    advertise.destroy()
  }

  static async detail(account,pwd) {
    const advertise = await UserLogin.scope('iv').findOne({
      where: {
        account,
        pwd
      }
    });
    if (!advertise) {
      throw new global.errs.NotFound('没有找到相关用户信息');
    }
    return advertise
  }

  // 更新广告
  static async update(id, v) {
    const login = await UserLogin.findByPk(id);
    if (!login) {
      throw new global.errs.NotFound('没有找到相关用户信息');
    }
    login.is_student = v.get('body.is_student')
    login.number = v.get('body.number')
    login.img = v.get('body.img')
    login.mail = v.get('body.mail')
    login.nickname = v.get('body.nickname')
    login.work_number = v.get('body.work_number')
    login.pwd = v.get('body.pwd')
    login.created_at = v.get('body.created_at')

    login.save();
  }


  // 验证密码
  static async verify(account,pwd) {
    // 查询用户是否存在
    const admin = await UserLogin.findOne({
      where: {
        account: account
      }
    })
    if (!admin) {
      throw new global.errs.AuthFailed('账号不存在')
    }
    console.log(admin.pwd);
    if(admin.pwd === pwd) {
       return admin
    }else{
      throw new global.errs.AuthFailed('密码不正确')
    }
    // const correct = bcrypt.compareSync(password, admin.password);
    // console.log(correct);
    // if (!correct) {
    //   throw new global.errs.AuthFailed('密码不正确')
    // }

    // return admin
  }

  // 验证密码
  static async userLogin(v) {
      console.log(v);
    // 查询用户是否存在
    const admin = await UserLogin.findOne({
      where: {
        account: v.account,
        password: v.password
      }
    })
    if (!admin) {
      throw new global.errs.AuthFailed('账号不存在或者密码不正确')
    }
    console.log(admin);
    return admin
  }

  // 查询管理员信息
  static async detail(id) {
    const scope = 'bh';
    // 查询管理员是否存在
    const admin = await UserLogin.scope(scope).findOne({
      where: {
        id
      }
    })

    if (!admin) {
      throw new global.errs.AuthFailed('账号不存在或者密码不正确')
    }

    return admin
  }

  // 广告列表
  static async list(page = 1) {
    const pageSize = 6;
    const advertise = await UserLogin.scope('bh').findAndCountAll({
      limit: pageSize,
      offset: (page - 1) * pageSize,
      where: {
        deleted_at: null
      },
      order: [
        ['created_at', 'DESC']
      ],
      attributes: {
        exclude: ['created_at']
      }
    })

    return {
      data: advertise.rows,
      meta: {
        current_page: parseInt(page),
        per_page: 10,
        count: advertise.count,
        total: advertise.count,
        total_pages: Math.ceil(advertise.count / 10),
      }
    };
  }
}

module.exports = {
    UserLoginDao
}
